package com.moon.admin.sys.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.moon.admin.framework.springmvc.config.SpringContextConfig;
import com.moon.admin.framework.util.ToolUtils;
import com.moon.admin.sys.entity.SysMenuEntity;
import com.moon.admin.sys.entity.SysRoleEntity;
import com.moon.admin.sys.entity.SysRoleMenuEntity;
import com.moon.admin.sys.mapper.SysRoleMapper;

/**
 * 角色Service
 * @author ninglong
 */
@Service
public class SysRoleService extends ServiceImpl<SysRoleMapper, SysRoleEntity> {

	@Autowired
	private SysRoleMenuService roleMenuService;
	
	/**
	 * 根据角色id获取其权限
	 * @param id 角色id
	 */
	public Map<String,Object> getMenuPermissionsById(Integer id){
		Map<String,Object> map = Maps.newHashMap();
		List<Integer> permissionsIds = roleMenuService.list(Wrappers.<SysRoleMenuEntity>lambdaQuery().eq(SysRoleMenuEntity::getRoleId, id))
				.stream().map(entity->entity.getMenuId()).collect(Collectors.toList());
		map.put("permissionsIds", permissionsIds);
		List<SysMenuEntity> list = SpringContextConfig.getBean(SysMenuService.class).list();
		Map<Integer, List<SysMenuEntity>> groupBy = list.stream().collect(Collectors.groupingBy(SysMenuEntity::getParentId));
		List<SysMenuEntity> menuDatas = getChildrenSysMenuOfPermissionsTree(groupBy,"",0);
    	map.put("menuDatas", menuDatas);
		return map;
	}
	
	/**
	 * 递归
	 */
	private List<SysMenuEntity> getChildrenSysMenuOfPermissionsTree(Map<Integer, List<SysMenuEntity>> groupBy, String href, Integer rootId){
		List<SysMenuEntity> result = Lists.newArrayList();
		List<SysMenuEntity> topMenuList = groupBy.get(rootId);
		if(topMenuList == null){return new ArrayList<SysMenuEntity>();}
		for(SysMenuEntity menuEntity:topMenuList){
//			menuEntity.setLabel(menuEntity.getMenuName());
			List<SysMenuEntity> tmpMenuList = groupBy.get(menuEntity.getId());
			if(tmpMenuList!=null && !tmpMenuList.isEmpty()){
				if(menuEntity.getMenuType().equals("C")){
					menuEntity.setFuns(getChildrenSysMenuOfPermissionsTree(groupBy, menuEntity.getPath(),menuEntity.getId()));
				}else{
					menuEntity.setChildren(getChildrenSysMenuOfPermissionsTree(groupBy, menuEntity.getPath(),menuEntity.getId()));
				}
			}
			result.add(menuEntity);
		}
		result = result.stream().sorted((s1,s2) -> s1.getOrderNum().compareTo(s2.getOrderNum())).collect(Collectors.toList());
		return result;
	}
	
	/**
	 * 保存功能权限
	 * @param roleId 角色id
	 * @param menuIds 功能菜单id
	 * @return
	 */
	@Transactional
	public void saveMenuPermission(Integer roleId,String menuIds){
		roleMenuService.remove(Wrappers.<SysRoleMenuEntity>lambdaQuery().eq(SysRoleMenuEntity::getRoleId, roleId));
		if(ToolUtils.isBlank(menuIds))return;
		List<Integer> idList = ToolUtils.toStrList(menuIds).stream().map(str->Integer.valueOf(str)).collect(Collectors.toList());
		List<SysRoleMenuEntity> list = Lists.newArrayList();
		for(Integer menuId:idList) {
			SysRoleMenuEntity roleMenuEntity = SysRoleMenuEntity.builder().roleId(roleId).menuId(menuId).build();
			list.add(roleMenuEntity);
		}
		roleMenuService.saveBatch(list);
	}
}